package com.jsits.v2x.bx05.service.impl;

import com.jsits.v2x.bx05.config.Bx05Config;
import com.jsits.v2x.bx05.server.Bx05ControlServer;
import com.jsits.v2x.bx05.service.Bx05LedDisplayService;
import com.jsits.v2x.common.dataClient.DataClient;
import com.jsits.v2x.common.dto.ControlDto;
import com.jsits.v2x.common.enums.DevicePosition;
import com.jsits.v2x.common.enums.TextContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class Bx05LedDisplayServiceImpl extends DataClient implements Bx05LedDisplayService, ApplicationListener<ApplicationReadyEvent> {
    @Autowired
    private Bx05Config bx05Config;

    /**
     * 获取各个显示屏需要显示的内容，并控制需要的屏幕显示
     */
    @Override
    public void updateLedScreen() {
        if(Bx05ControlServer.bx05Config == null) {
            return;
        }

        List<ControlDto> controlDtos = GetControlStatus(bx05Config.getDataUrl());
        if(controlDtos != null) {
            // 控制每个屏幕显示
            for (ControlDto controlDto : controlDtos) {
                Bx05ControlServer.sendCtrontrolData(controlDto);
            }
        }
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        Bx05ControlServer.bx05Config = bx05Config;
        Bx05ControlServer.init();

        //向屏幕中写入节目内容
        //Bx05ControlServer.writePrograms();

        //测试屏幕显示的内容
//        ControlDto controlDto = new ControlDto();
//        controlDto.setDevicePosition(DevicePosition.DEVICE_POSITION_MAIN_1);
//        controlDto.setTextContent(TextContent.TEXT_CONTENT_MAIN_OVER_MAN);
//        Bx05ControlServer.sendCtrontrolData(controlDto);
    }
}
